import urllib, re, xbmcplugin, xbmcgui, os

#.nF0
__plugin__ = "Fukung"
__author__ = "Anders Brujordet"
__date__ = "06 01 2010"
__version__ = "1"

handle = sys.argv[1]
dialog = xbmcgui.Dialog()

def list_pics():
    LIMIT = 4
    counter = 1
    linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')

    while counter < LIMIT:
        data = urllib.urlopen("http://fukung.net/nsfw/page," + str(counter)).read()
        links = linkregex.findall(data)
        
        for link in links:
            if link[0] == "/" and link[1] == "v" and "gif" not in link:
                picId = link.split("/")[2]
                pic = "http://media.fukung.net/images/" + picId + ".gif"
                listitem = xbmcgui.ListItem( os.path.basename(pic), iconImage="DefaultPicture.png", thumbnailImage=pic.replace("images", "thumbs"))
                listitem.setInfo( type="Picture", infoLabels={"Title": os.path.basename(pic)})
                xbmcplugin.addDirectoryItem( handle=int(handle), url=pic, listitem=listitem, isFolder=False, totalItems=(LIMIT*7))
        counter += 1

list_pics()
xbmcplugin.addSortMethod( handle=int(handle), sortMethod=xbmcplugin.SORT_METHOD_NONE)
xbmcplugin.endOfDirectory(int(handle))
